// ignore_for_file: unnecessary_overrides

import 'package:get/get.dart';
import 'package:native_dart_package/http/request/exception/exception_handler.dart';
import 'package:native_dart_package/native_dart_package.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/pages/mine/message_center/message_center_model.dart';
import 'package:yt_devops/routers/router_path.dart';
import 'package:yt_devops/services/messsage/message_services.dart';
import 'package:yt_devops/utils/enums.dart';

class MessageCenterController extends GetxController {
  final MessageCenterState state = MessageCenterState();
  final MessageServices messageServices = Get.put(MessageServices());
  bool hasUnread = false;
  Map<String, dynamic> messages = {};
  List messagesList = [];

  @override
  void onReady() {
    super.onReady();

    getAnnouncementList();
  }

  /// 去公告消息页
  void goAdvertiseMessage() {
    Get.toNamed(RouterPath.advertiseMessage);
  }

  /// 去任务详情页
  void goToDoTasks() {
    // Get.toNamed(RouterPath.advertiseMessage);
  }

  /// 请求公告列表
  void getAnnouncementList() async {
    var params = {'current': 1, 'pageSize': 10,'noticeType': 2};
    messages =
        await messageServices.getAnnouncementList(params, onError: (err) {
      handleException(err);
      return false;
    });
    Log.d('messages===' + messages.toString());
    judgeAdvertiseStatus();
  }

  /// 判断公告状态
  void judgeAdvertiseStatus() {
    if (messages['total'] == 0) return;
    if (messages != null && messages.containsKey('list') && messages['list'] is List) {
      hasUnread = messages['list'].any((element) {
        return element['status'] == AdvertiseStatusType.normal.value;
      });
    }else{
      hasUnread = true;
    }

    update();
  }
}
